package com.chenzhou.product_sys.controller;

import com.chenzhou.product_sys.entity.GoodsEntity;
import com.chenzhou.product_sys.entity.GoodsTypeEntity;
import com.chenzhou.product_sys.entity.ImagesEntity;
import com.chenzhou.product_sys.service.GoodsService;
import com.chenzhou.product_sys.service.ImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("/goods_image")
public class GoodsImageController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ImagesService imagesService;

    @RequestMapping("/queryGoodsImage")
    public String bind_image(HttpServletRequest request){
        List<GoodsEntity> list = goodsService.queryAllGoods();
        request.setAttribute("glist",list);
        request.setAttribute("menu","images");
        return "goods_image/show_images";
    }

    @RequestMapping("/queryGoodsImageOne")
    public String queryGoodsOne(HttpServletRequest request,Integer id){
        GoodsEntity goodsEntity = goodsService.queryGoodsByID(id);
        List<ImagesEntity> list = imagesService.queryImagesByGoodsID(id);
        request.setAttribute("imglist",list);
        request.setAttribute("goods",goodsEntity);
        request.setAttribute("menu","images");
        return "goods_image/images_upload";
    }

    @RequestMapping("/upload_image")
    public String queryGoodImage(HttpServletRequest request, Integer id, MultipartFile picture){
        Random random = new Random(100000);
        String basePath = "D:\\image_server\\nginx-1.19.0\\html\\imgs\\";
        String filename = picture.getOriginalFilename();
        //123.jpg
        String prefix_name = UUID.randomUUID().toString();
        String suffix_name = filename.substring(filename.lastIndexOf('.'));

        File file = new File(basePath+prefix_name+suffix_name);
        try {
            picture.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        ImagesEntity imagesEntity = new ImagesEntity();
        imagesEntity.setFilename(basePath+prefix_name+suffix_name);
        imagesEntity.setFile_url("http://localhost/imgs/"+prefix_name+suffix_name);
        imagesEntity.setFile_pix(suffix_name);
        imagesEntity.setGood_id(id);
        imagesEntity.setMarkup("刚刚添加了一个图片信息");
        imagesService.add_Goods(imagesEntity);
        return this.queryGoodsOne(request,id);
    }

    @RequestMapping("/delete_image")
    public String delete_goodsImage(HttpServletRequest request, Integer id){
        ///TODO 删除在nginx文件夹下的图片，执行物理删除

        int a = imagesService.delete_goodsImage(id);
        if(a>0){
            return  this.queryGoodsOne(request,id);//防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }
}
